package arrAndstr;

/*
    209. 长度最小的子数组-滑动窗口
 */
public class T209 {

    public static void main(String[] args) {
        int target = 7;
        int[] nums = new int[]{2,3,1,2,4,3};

        System.out.println(minSubArrayLen(target, nums));
    }

    public static int minSubArrayLen(int target, int[] nums) {
        int len = nums.length;
        int result = len+1;
        int sum = 0;
        int leftIndex = 0;
        for(int rightIndex=leftIndex; rightIndex<len; rightIndex++) {
            sum += nums[rightIndex];
            if(sum < target) {
                continue;
            }
            // sum >= target
            while(sum >= target) {  // 不用怕left与right的问题，因为sum会减到0，而target大于0
                int tempResult = rightIndex-leftIndex+1;
                result = result <= tempResult ? result : tempResult;
                sum -= nums[leftIndex++];
            }
        }

        return result==len+1 ? 0 : result;
    }
}
